package org.tong.jpa.repo;

import com.github.wenhao.jpa.Specifications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;
import org.tong.jpa.entities.Student;

import java.util.List;
import java.util.Optional;

/**
 * @author
 * @date 2022-08-01 16:45
 */
@Repository
public class StudentRepoImpl {

    @Autowired
    private StudentJpaRepo studentRepo;

    public Student findByName(String name) {
        Specification<Student> build = Specifications.<Student>and()
                .eq("name", name)
                .build();
        Optional<Student> one = studentRepo.findOne(build);
        return one.orElse(null);
    }

    public List<Student> findByNameLike(String name) {
        Specification<Student> build = Specifications.<Student>and()
                .like("name", name + "%")
                .build();
        return studentRepo.findAll(build);
    }


    public Student findStudentById(Long id) {
        return studentRepo.findById(id).orElse(null);
    }

    public List<Student> findByNames(List<String> names) {
        Specification specification = Specifications.and()
                .in("name", names.toArray())
                .build();
        return studentRepo.findAll(specification);
    }
}

